package com.cn.xact.ty_treasured_book.activity;

import java.io.File;
import java.util.ArrayList;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.TYApplicationState;
import com.cn.xact.ty_treasured_book.config.TYConfigBase;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.sqlite.TYDBManager;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class SplashScreen extends Activity {

    private ViewPager viewPager;
    private ArrayList<View> pageViews;
    private ViewGroup main, group;
    private ImageView imageView;
    private ImageView[] imageViews;

    private NetworkImpl mInitNetwork;

    private TYDBManager mDBManager;

    private String[] mSupportedPhones;

    private TYApplicationState mAppState;

    private View addViews(int resId) {
        ImageView iv = new ImageView(this);
        iv.setImageResource(resId);
        return iv;
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = getLayoutInflater();
        pageViews = new ArrayList<View>();
        pageViews.add(addViews(R.drawable.froyo));
        pageViews.add(addViews(R.drawable.gb));
        pageViews.add(addViews(R.drawable.ics));
        pageViews.add(addViews(R.drawable.jb));
        pageViews.add(inflater.inflate(R.layout.splash_end, null));

        imageViews = new ImageView[pageViews.size()];
        main = (ViewGroup)inflater.inflate(R.layout.splash, null);

        group = (ViewGroup)main.findViewById(R.id.adViews);

        viewPager = (ViewPager)main.findViewById(R.id.guidePages);

        for (int i = 0; i < pageViews.size(); i++) {
            imageView = new ImageView(SplashScreen.this);
            imageView.setLayoutParams(new LayoutParams(20,20));
            imageView.setPadding(20, 0, 20, 0);
            imageViews[i] = imageView;
            if (i == 0) {
                imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
            } else if (i != (pageViews.size() - 1)) {
                imageViews[i].setBackgroundResource(R.drawable.page_indicator);
            }
            group.addView(imageViews[i]);
        }

        setContentView(main);

        viewPager.setAdapter(new GuidePageAdapter());
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());

        mDBManager = new TYDBManager(this);
        mAppState = new TYApplicationState(this);

        mInitNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        mInitNetwork.getSupportPhonesAsync(123);
        mInitNetwork.getUserInfoAsync(123);

        prepareFileSystem();
    }

    private void prepareFileSystem() {

        File root    = new File(TYConfigBase.TY_LOCAL_DIR);
        File app     = new File(TYConfigBase.TY_LOCAL_DIR + TYConfigBase.TY_LOCAL_APP_INFO_DIR);
        File appMail = new File(TYConfigBase.TY_LOCAL_DIR
                + TYConfigBase.TY_LOCAL_APP_INFO_DIR
                + TYConfigBase.TY_LOCAL_APP_INFO_DIR_MAIL_CALENDAR);
        if (!root.exists()) {
            root.mkdir();
        }

        if (!app.exists()) {
            app.mkdir();
        }

        if (!appMail.exists()) {
            appMail.mkdir();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDBManager.closeDB();
    }

    class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }
    }

    class GuidePageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < (imageViews.length - 1); i++) {
                if (arg0 != (imageViews.length - 1)) {
                    imageViews[arg0]
                            .setBackgroundResource(R.drawable.page_indicator_focused);
                    if (arg0 != i) {
                        imageViews[i]
                                .setBackgroundResource(R.drawable.page_indicator);
                    }
                }
            }

            int next = arg0 + 1;
            if (next == imageViews.length) {
                if (mAppState.getCurPhone() == null) {
                    Intent tmp = new Intent(SplashScreen.this, HomePhoneSelActivity.class);
                    tmp.putExtra("phones", mSupportedPhones);
                    tmp.putExtra("bootmain", true);
                    startActivity(tmp);
                } else {
                    Intent tmp = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(tmp);
                }

                SplashScreen.this.finish();
            }

        }
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(SplashScreen.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_LOGIN:
                if (!success) {
                    //TODO: Login failed here
                }
                break;
            case NetworkImpl.NET_WORK_ID_GET_SUPPORT_PHONES:
                mSupportedPhones = (String[])attach1;
                TYApplicationState.updateSupportedPhones(mSupportedPhones);
                break;
            }
        }
    }
}
